

class MyPromise {

    constructor(executor) {
        executor(this.resolve, this.reject)
    }

    status = 'PENDING'  //状态

    value = ''

    reason = ''

    resolve = (value) => {
        if (this.status !== 'PENDING') return;
        this.status = 'FULFILLED';
        this.value = value;
        //成功回调
        this.successCB && this.successCB(this.value)
    }

    reject = (value) => {
        if (this.status !== 'PENDING') return;
        this.value = value;
        this.status = 'REJECTED';

        
        //失败回调
        this.failCallback && this.failCallback(this.value)
    }

    then = (successCB, failCB) => {
        if (this.status === 'FULFILLED') {
            successCB(this.value);
        } else if (this.status === 'REJECTED') {
            failCB(this.value);
        } else {
            //异步  等待状态
            this.successCB = successCB;
            this.failCB = failCB;
        }

    }

}

module.exports = MyPromise